package com.yqg.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.education.entity.TeachPlan;
import com.yqg.education.mapper.LessonNoteMapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.education.common.Result;

import com.yqg.education.service.ILessonNoteService;
import com.yqg.education.entity.LessonNote;

import org.springframework.web.bind.annotation.RestController;

/**
 * LessonNoteController 前端控制器
 * @author zxf
 * @since 2025-09-13
 */
@RestController
@RequestMapping("/lesson-note")
public class LessonNoteController {

    @Resource
    private ILessonNoteService lessonNoteService;

    @Resource
    private LessonNoteMapper lessonNoteMapper;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    public Result save(@RequestBody LessonNote lessonNote) {
        lessonNote.setCreateTime(LocalDateTime.now());
        return Result.success(lessonNoteService.saveOrUpdate(lessonNote));
    }
    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        LessonNote lessonNote =lessonNoteService.getById(id);
        lessonNote.setIsDelete(1);
        return Result.success(lessonNoteService.saveOrUpdate(lessonNote));
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<LessonNote> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
       List<LessonNote> list=  lessonNoteService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(lessonNoteService.getById(id));
    }
    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           String name,
                           Integer courseId) {
        pageNum = pageNum * pageSize - pageSize;
        List<LessonNote> lessonNoteList = lessonNoteMapper.pageList(pageNum, pageSize, name, courseId);
        Long count = lessonNoteMapper.findCount(name, courseId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("records", lessonNoteList);
        map.put("total", count);
        return Result.success(map);
    }

    @GetMapping("/findByCourseId")
    public Result findByCourseId(@RequestParam Integer courseId) {
        LambdaQueryWrapper<LessonNote> lessonNoteLambdaQueryWrapper = new LambdaQueryWrapper<>();
        lessonNoteLambdaQueryWrapper.eq(LessonNote::getCourseId, courseId);
        lessonNoteLambdaQueryWrapper.eq(LessonNote::getIsDelete, 0);
        return Result.success(lessonNoteService.list(lessonNoteLambdaQueryWrapper));
    }
}

